package com.wiz.web.service.impl;

import io.netty.channel.Channel;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wiz.common.NettyConstant;
import com.wiz.common.ResponseCode;
import com.wiz.data.vo.to.MabDataTO;
import com.wiz.web.dao.mapper.HqStationMapper;
import com.wiz.web.dao.mapper.SyncMapper;
import com.wiz.web.manager.ServiceManager;
import com.wiz.web.service.ApiService;
import com.wiz.web.vo.criteria.HqStationCriteria;
import com.wiz.web.vo.to.AdminTO;
import com.wiz.web.vo.to.HqTO;
import com.wiz.web.vo.to.NoticeMessageTO;
import com.wiz.web.vo.to.PageTO;
import com.wiz.web.vo.to.PriceMessageTaskTO;
import com.wiz.web.vo.to.StationTO;
import com.wiz.web.vo.type.IssuedType;

@Service("apiService")
public class ApiServiceImpl extends BasicServiceImpl implements ApiService,ResponseCode,NettyConstant {
//	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private HqStationMapper hqStationMapper;
	@Resource
	private SyncMapper syncMapper;
	
	@Override
	public String getStationOnlineInfo(List<HqStationCriteria> criteriaList) {
		for(HqStationCriteria criteria:criteriaList){
			int status = serviceManager.getStationConnectMap().containsKey(criteria.getStationId())?1:0;
			criteria.setStatus(status);
		}
		PageTO page = new PageTO();
		page.setDataList(criteriaList);
		return returnResult(CODE_OK, STRING_OK, page);
	}
	
	/**
	 * 下发通知
	 */
	private String issuedNoice(String stationId, String updateTime) throws Exception {
		Channel channel = serviceManager.getStationConnectMap().get(stationId);
		if(channel == null){
			return RESPONSE_CODE_STATION_OFFLINE;
		}
		List<NoticeMessageTO> msgList = syncMapper.syncNoticeMessage(stationId, updateTime);
		for(NoticeMessageTO to:msgList){
			MabDataTO mabDataTO = new MabDataTO();
			mabDataTO.setWiz_1_dataType("O0");;
			mabDataTO.setWiz_2_time(to.getCreatedTime().getTime());
			mabDataTO.setWiz_54_spareLString(String.valueOf(to.getId()));
			mabDataTO.setWiz_59_spareLString(to.getTitle());
			mabDataTO.setWiz_62_message(to.getContent());
			channel.writeAndFlush(getNettyMessage(mabDataTO, "issuedNoice", stationId));
		}
		return RESPONSE_CODE_OK;
	}

	/**
	 * 根据总部Id,操作时间查询调价任务
	 * @param hqId
	 * @param operateTime
	 * @return
	 * @throws Exception
	 */
	private void issuedPriceMessageTask(String hqId,String operateTime)
			throws Exception {
		List<PriceMessageTaskTO> list = hqStationMapper.getPriceMessageTaskHistory(hqId, operateTime);
		for(PriceMessageTaskTO to:list){
			String stationId = to.getStationId();
			MabDataTO mabDataTO = new MabDataTO();
			mabDataTO.setWiz_1_dataType("O1");
			mabDataTO.setWiz_2_time(to.getCreatedTime().getTime());
			mabDataTO.setWiz_3_endTime(to.getOperateTime().getTime());
			mabDataTO.setWiz_11_stationId(Integer.parseInt(stationId));
			mabDataTO.setWiz_17_prId((long)to.getPrId());
			mabDataTO.setWiz_27_prName(to.getPrName());
			mabDataTO.setWiz_45_price(to.getPrice());
			mabDataTO.setWiz_46_priceOther(to.getCurrentPrice());
			mabDataTO.setWiz_62_message(String.valueOf(to.getId()));
			addTransmissionTask(stationId,"PMT"+to.getId(), 
					getNettyMessage(mabDataTO, "issuedPriceMessageTask", stationId),1000);
		}
	}

	@Override
	public void handleResquest(String stationId, MabDataTO mabTO,
			ServiceManager serviceManager) {
		String dataType = mabTO.getWiz_1_dataType();
		if(dataType.endsWith("1")){//调价任务确认接收
			int status = Integer.parseInt(mabTO.getWiz_60_status());
			int id = Integer.parseInt(mabTO.getWiz_62_message());
			hqStationMapper.updatePriceMessageTask(id, status);
			serviceManager.removeTransmissionTask(stationId, "PMT"+id);
		}
	}

	/**
	 * 下行站点信息
	 * @param stationId
	 * @return
	 * @throws Exception
	 */
	private String issuedStation(String stationId) throws Exception {
		Channel channel = serviceManager.getStationConnectMap().get(stationId);
		if(channel == null){
			return RESPONSE_CODE_STATION_OFFLINE;
		}
		StationTO to = syncMapper.syncStationDetail(stationId, null);
		MabDataTO mabDataTO = new MabDataTO();
		mabDataTO.setWiz_1_dataType("S1");
		mabDataTO.setWiz_50_spareLString(gson.toJson(to));
		channel.writeAndFlush(getNettyMessage(mabDataTO, "issuedStation", stationId));
		return RESPONSE_CODE_OK;
	}

	/**
	 * 下行权限
	 * @param stationId
	 * @param updateTime
	 * @return
	 * @throws Exception
	 */
	private String issuedPermission(String stationId, String updateTime)
			throws Exception {
		Channel channel = serviceManager.getStationConnectMap().get(stationId);
		if(channel == null){
			return RESPONSE_CODE_STATION_OFFLINE;
		}
		List<AdminTO> list = syncMapper.syncAdminClientPermission(stationId,updateTime);
		MabDataTO mabDataTO = new MabDataTO();
		for(AdminTO to:list){
			mabDataTO.setWiz_1_dataType("P1");
			mabDataTO.setWiz_51_spareLString(gson.toJson(to));
			channel.writeAndFlush(getNettyMessage(mabDataTO, "issuedPermission", stationId));
		}
		return RESPONSE_CODE_OK;
	}

	/**
	 * 下行员工卡
	 * @param stationId
	 * @param updateTime
	 * @return
	 * @throws Exception
	 */
	private String issuedAdminCard(String stationId, String updateTime)
			throws Exception {
		Channel channel = serviceManager.getStationConnectMap().get(stationId);
		if(channel == null){
			return RESPONSE_CODE_STATION_OFFLINE;
		}
		List<AdminTO> list = syncMapper.syncAdminCard(stationId, updateTime);
		for(AdminTO to:list){
			MabDataTO mabDataTO = new MabDataTO();
			mabDataTO.setWiz_1_dataType("C1");
			mabDataTO.setWiz_52_spareLString(gson.toJson(to));
			channel.writeAndFlush(getNettyMessage(mabDataTO, "issuedAdminCard", stationId));
		}
		return RESPONSE_CODE_OK;
	}

	/**
	 * 下行员工登录信息
	 * @param stationId
	 * @param updateTime
	 * @return
	 * @throws Exception
	 */
	private String issuedAdminLoginInfo(String stationId, String updateTime) throws Exception{
		Channel channel = serviceManager.getStationConnectMap().get(stationId);
		if(channel == null){
			return RESPONSE_CODE_STATION_OFFLINE;
		}
		List<AdminTO> list = syncMapper.syncAdminLoginInfo(stationId, updateTime);
		for(AdminTO to:list){
			MabDataTO mabDataTO = new MabDataTO();
			mabDataTO.setWiz_1_dataType("L1");
			mabDataTO.setWiz_53_spareLString(gson.toJson(to));
			channel.writeAndFlush(getNettyMessage(mabDataTO, "issuedAdminLogin", stationId));
		}
		return RESPONSE_CODE_OK;
	}
	
	private String issuedPayConfig(String hqId, String stationId, String updateTime) throws Exception {
		Channel channel = serviceManager.getStationConnectMap().get(stationId);
		if(channel == null){
			return RESPONSE_CODE_STATION_OFFLINE;
		}
		List<HqTO> list = syncMapper.syncPayTypeConfig(hqId, updateTime);
		for(HqTO to:list){
			MabDataTO mabDataTO = new MabDataTO();
			mabDataTO.setWiz_1_dataType("M1");
			mabDataTO.setWiz_54_spareLString(gson.toJson(to));
			channel.writeAndFlush(getNettyMessage(mabDataTO, "issuedPayConfig", stationId));
		}
		return RESPONSE_CODE_OK;
	}
	
	private String issuedPrConfig(String hqId, String stationId, String updateTime) throws Exception {
		Channel channel = serviceManager.getStationConnectMap().get(stationId);
		if(channel == null){
			return RESPONSE_CODE_STATION_OFFLINE;
		}
		List<HqTO> list = syncMapper.syncPrConfig(hqId, updateTime);
		for(HqTO to:list){
			MabDataTO mabDataTO = new MabDataTO();
			mabDataTO.setWiz_1_dataType("R1");
			mabDataTO.setWiz_55_spareLString(gson.toJson(to));
			channel.writeAndFlush(getNettyMessage(mabDataTO, "issuedPrConfig", stationId));
		}
		return RESPONSE_CODE_OK;
	}

	@Override
	public String issued(String hqId,String stationId, Integer type, String updateTime)
			throws Exception {
		if(IssuedType.STATION_ADMIN_CARD.getId() == type){
			return issuedAdminCard(stationId,updateTime);
		}else if(IssuedType.STATION_ADMIN_LOGIN_INFO.getId() == type){
			return issuedAdminLoginInfo(stationId, updateTime);
		}else if(IssuedType.STATION_BASE_INFO.getId() == type){
			return issuedStation(stationId);
		}else if(IssuedType.STATION_CLIENT_PERMISSION.getId() == type){
			return issuedPermission(stationId, updateTime);
		}else if(IssuedType.HQ_PAID_TYPE_CONFIG.getId() == type){
			return issuedPayConfig(hqId, stationId, updateTime);
		}else if(IssuedType.STATION_NOTIFIED.getId() == type){
			return issuedNoice(stationId, updateTime);
		}else if(IssuedType.PR_PRICE.getId() == type){
			issuedPriceMessageTask(hqId, updateTime);
		}else if(IssuedType.HQ_PR_CONFIG.getId() == type){
			return issuedPrConfig(hqId, stationId, updateTime);
		}
		return RESPONSE_CODE_OK;
	}

}
